<?php

namespace app\api\services;

/**
 * Trait ApiTrait
 * @package app\api\services\cainiao
 * @desc 公共处理
 */
trait ApiTrait
{
    public $fileName = 'jizhan.log';

    /**
     * @desc 记录日志到日志文件
     * @param string $fileName 日志文件名称[a.log]
     * @param string $content 日志内容
     */
    public function writeLog($fileName , $content)
    {
        if(empty($fileName)){
            $fileName = $this->fileName;
        }
        // todo list
        rwf($fileName , $content);
    }

    // curl post请求

    /**
     * @desc curl post请求
     * @param string $url 接口url地址
     * @param string $data json字符串内容
     * @return bool|string
     */
    public function curlPost($url, $data , $header = 1)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($header == 1){
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_POST, 1);
        $output = curl_exec($ch);

        curl_close($ch);
        return $output;
    }

    // 下发数组数据
    public function returnArr($code = 0 , $msg = '失败' , $data = [])
    {
        return compact('code' , 'msg' , 'data');
    }

    // 下发json数据
    public function returnJson($code = 0 , $msg = '失败' , $data = [])
    {
        return json($this->returnArr($code , $msg , $data));
    }

    /**
     * @desc curl get请求
     * @param string $url 地址
     * @param int $type 类型,是否返回完整
     * @return bool|int|string
     */
    public function curlGet($url, $type = 1)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_URL, $url);
        if ($type == 1) {
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        }
        $res = curl_exec($curl);
        if ($res) {
            curl_close($curl);
            return $res;
        } else {
            $error = curl_errno($curl);
            curl_close($curl);
            return $error;
        }
    }

    /**
     * @desc curl 请求
     * @param string $url 请求url
     * @param array $data 请求数据
     * @param string $method 请求方法POST
     * @return mixed
     */
    public function httpCurl($url, $data = [], $method = 'GET')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($method == "POST") {
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
//        $error = curl_errno($ch);
//        halt('getCurlJson',$url,$data,$method,$error,$output);
        curl_close($ch);;
        return $output;
    }
}